package com.flash.DayProcessing.mapper;

import com.flash.DayProcessing.entity.GoodName;
import com.flash.InventoryManagement.entity.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface InventoryStatisticsMapper {
    @Select("\n" +
            "select nvl(s.securityId,d.securityId) as securityId,\n" +
            "nvl(s.fundId,d.fundId) as fundId,\n" +
            "(case d.dealway when 1 then nvl(s.quantity,0)+nvl(d.dealNumber,0)\n" +
            " when 2 then (nvl(s.quantity,0)-nvl(d.dealNumber,0))end) as quantity,\n" +
            " (case d.dealway when 1 then (nvl(s.money,0)+nvl(d.dealMoney,0))\n" +
            " when 2 then (nvl(s.money,0)-nvl(s.unitCost,0)*nvl(s.quantity,0))  end) as money, \n" +
            " case d.dealway when 1 then (nvl(s.money,0)+nvl(d.dealMoney,0))/(nvl(s.quantity,0)+nvl(d.dealNumber,0)) \n" +
            " when 2 then s.unitCost end as unitCost\n" +
            "from (select * from securitystock where dateStock = #{dateStock}) s \n" +
            "full join (select * from dealData where dealTime = #{dealTime})d on s.securityId=d.securityId \n"
            )
    public List<SecurityStock> securityStockAndDealData(String dealTime,String dateStock);

    @Select("select nvl(c.cashId,f.cashId) as cashId, " +
            " nvl(c.fundId,f.fundId) as fundId," +
            "(case f.allotDirection when 1 then nvl(c.money,0)+nvl(f.allotMoney,0)" +
            "else nvl(c.money,0)-nvl(f.allotMoney,0) end) as money " +
            "from (select * from cashstock where dateCashStock=#{dateCashStock})c " +
            "full join (select * from fundsAllot where businesstime=#{businesstime})f on c.cashId = f.cashId")
    public List<CashStock> cashStockAndFundsAllot(String dateCashStock, String businesstime);

    @Select("\n" +
            "select nvl(ss.securityId,sa.securityId) as securityId,\n" +
            "nvl(ss.fundId,sa.fundId) as fundId,\n" +
            "sa.fundFlow as businessState,\n" +
            "case sa.fundFlow when 1 then nvl(ss.money,0)+ nvl(sa.money,0)\n" +
            "else nvl(ss.money,0) - nvl(sa.money,0) end as money\n" +
            " from (select * from securityAccrualStock where dateAccrualStock = #{dateAccrualStock}) ss\n" +
            " full join (select * from securityAccrued where businessTime = #{businessTime}) sa\n" +
            " on ss.securityAccrualStockId = sa.securityAccruedId\n")
    public List<SecurityAccrualStock> securityAccrualStockAndSecurityAccrued(String dateAccrualStock,String businessTime);

    @Select("\n" +
            "select nvl(cas.cashId,ca.cashId) cashId,\n" +
            "nvl(cas.remark,ca.fundId) remark,\n" +
            "nvl(cas.businessType,ca.businessType) businessType,\n" +
            "nvl(cas.businessState,ca.fundFlow) businessState,\n" +
            "case ca.fundFlow when 1 then nvl(cas.money,0)+nvl(ca.money,0)\n" +
            "else nvl(cas.money,0) - nvl(ca.money,0) end money\n" +
            "from (select * from cashAccrued where businessTime = #{businessTime}) ca\n" +
            "full join (select * from cashAccrualStock where dateAccrualStock = #{dateAccrualStock}) cas\n" +
            "on cas.cashId = ca.cashAccruedId\n")
    public List<CashAccrualStock> cashAccrualStockAndCashAccrued(String businessTime,String dateAccrualStock);
    @Select("\n" +
            "select nvl(t.fundId,ts.fundId) as fundId,\n" +
            "nvl(t.quantity,ts.quantity) as quantity,\n" +
            "case t.dealType when 2 then nvl(ts.money,0) - nvl(t.money,0)\n" +
            "else nvl(ts.money,0)+nvl(t.money,0) end as money\n" +
            "from (select * from TAStock where dateTAStock = #{dateTAStock}) ts\n" +
            "full join (select * from TA where dealTime = #{dealTime}) t on ts.fundId = t.fundId")
    public List<TaStock> taStockAndTA(String dateTAStock,String dealTime);

    @Delete("delete from ${table} where fundId = #{fundId} and ${tableTime} = #{time}")
    public int delete(String table,String fundId,String tableTime,String time);
    @Delete("delete from ${table} where remark = #{fundId} and ${tableTime} = #{time}")
    public int deletete(String table,String fundId,String tableTime,String time);

}
